package com.ship.dispatch.util;

import java.lang.reflect.Field;

public class PropertiesUtil {
    public static void copyProperties(Object source, Object target) throws IllegalAccessException {
        if (source == null || target == null) {
            return;
        }
        Class<?> sourceClass = source.getClass();
        Class<?> targetClass = target.getClass();
        Field[] sourceFields = sourceClass.getDeclaredFields();
        for (Field sourceField : sourceFields) {
            sourceField.setAccessible(true);
            String fieldName = sourceField.getName();
            try {
                Field targetField = targetClass.getDeclaredField(fieldName);
                targetField.setAccessible(true);
                Object oldValue = targetField.get(target);
                if(sourceField.get(source)!=null){
                    targetField.set(target, sourceField.get(source));
                }
            } catch (NoSuchFieldException e) {
                // 目标对象中不存在该属性，忽略或可以做特殊处理
            }
        }
    }
}
